// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WebcamRGBCameraModelAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Visualization
{
    using System;
    using System.Runtime.Serialization;    
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.Runtime.Cameras;
    using Microsoft.Robotics.Visualization;

    /// <summary>
    /// This model agent class prepares the RGB frame for visualization.
    /// </summary>
    [DataContract]
    public sealed class WebcamRGBCameraModelAgent : ModelAgent<RGBCameraAgentMessage, WebcamRGBCameraModel>
    {
        /// <summary>
        /// Select the type of display
        /// </summary>
        [DataMember(Name = "ImageFormat")]
        private SimplePixelFormat? imageFormat;

        /// <summary>
        /// Initializes a new instance of the WebcamRGBCameraModelAgent class        
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producer">The name of the agent we subscribe to.</param>
        /// <param name="maxUpdateIntervalMilliseconds">The interval on which to issue data binding updates to the UI</param>
        /// <param name="imageFormat">Image format</param>
        public WebcamRGBCameraModelAgent(
            string name,
            IProducer<RGBCameraAgentMessage> producer,
            int maxUpdateIntervalMilliseconds,
            SimplePixelFormat imageFormat)
            : base(name, producer.Name, maxUpdateIntervalMilliseconds)
        {
            this.imageFormat = (SimplePixelFormat)imageFormat;
        }

        /// <summary>
        /// Initializes any fields not provided in the manifest.
        /// </summary>
        /// <param name="locator">Not used.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);
         
            if (this.imageFormat == null)
            {
                this.imageFormat = SimplePixelFormat.Rgb24;
            }
        }

        /// <summary>
        /// Updates the model data with the new state.
        /// </summary>
        /// <param name="state">The new state from the upstream agent.</param>
        /// <returns>true if the model was updated, false otherwise</returns>
        protected override bool UpdateModel(RGBCameraAgentMessage state)
        {
            ImageFrameRgb rgbImage = ImageOperations2DInterop.ConvertYUY2ToRGBImage(state.RGBImageFrame);
            if (null == rgbImage)
            {
                return false;
            }

            System.Drawing.Size rgbSize = new System.Drawing.Size(state.RGBImageFrame.Width, state.RGBImageFrame.Height);

            if (this.imageFormat == SimplePixelFormat.Rgb24)
            {
                this.Model.VideoFrame = new VideoFrame()
                {
                    Data = rgbImage.ImageData,
                    Size = rgbSize,
                    Format = SimplePixelFormat.Rgb24,
                    Stride = rgbSize.Width * rgbImage.BytesPerPixel
                };
            }
            else
            {
                throw new Exception("RGBCamera - unsupported image type " + this.imageFormat.ToString());
            }

            return true;
        }
    }
}
